package com.zretc.device.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import com.zretc.dbtools.RandomCodeTools;
import com.zretc.device.dao.AdminDao;
import com.zretc.device.dao.Device_AdminDao;
import com.zretc.device.dao.UserDao;
import com.zretc.device.entity.Device_Admin;
import com.zretc.device.entity.User;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class UserLoginFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1328374552444655313L;
	private JPanel contentPane;
	private JTextField textUsername;
	private JTextField txtRanCode;
	private JTextField txtRan;

	private JPasswordField textPassword;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
					UserLoginFrame frame = new UserLoginFrame();
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserLoginFrame() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 661, 541);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("仓库管理系统");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 35));
		lblNewLabel.setBounds(202, 28, 228, 43);
		contentPane.add(lblNewLabel);

		JRadioButton radioButton_user = new JRadioButton("普通员工", true);
		radioButton_user.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		radioButton_user.setFont(new Font("宋体", Font.PLAIN, 18));
		radioButton_user.setBounds(354, 288, 123, 30);
		contentPane.add(radioButton_user);

		JRadioButton radioButton_dadmin = new JRadioButton("仓库员工");
		radioButton_dadmin.setFont(new Font("宋体", Font.PLAIN, 18));
		radioButton_dadmin.setBounds(244, 288, 115, 30);
		contentPane.add(radioButton_dadmin);

		JRadioButton radioButton_admin = new JRadioButton("管理员");
		radioButton_admin.setFont(new Font("宋体", Font.PLAIN, 18));
		radioButton_admin.setBounds(149, 288, 94, 30);
		contentPane.add(radioButton_admin);

		// 创建组
		ButtonGroup group = new ButtonGroup();
		// 将按钮添加到同一组中
		group.add(radioButton_user);
		group.add(radioButton_dadmin);
		group.add(radioButton_admin);

		textUsername = new JTextField();
		textUsername.setFont(new Font("宋体", Font.PLAIN, 25));
		textUsername.setBounds(255, 112, 200, 30);
		contentPane.add(textUsername);
		textUsername.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("用户名");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 25));
		lblNewLabel_1.setBounds(149, 111, 80, 33);
		contentPane.add(lblNewLabel_1);

		textPassword = new JPasswordField();
		textPassword.setBounds(255, 177, 200, 30);
		contentPane.add(textPassword);

		txtRanCode = new JTextField();
		txtRanCode.setFont(new Font("宋体", Font.PLAIN, 25));
		txtRanCode.setColumns(10);
		txtRanCode.setBounds(255, 240, 90, 30);
		contentPane.add(txtRanCode);

		JLabel label_1 = new JLabel("验证码");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(149, 237, 80, 33);
		contentPane.add(label_1);

		txtRan = new JTextField();
		txtRan.setText(RandomCodeTools.getRan());
		txtRan.setEditable(false);
		txtRan.setFont(new Font("宋体", Font.PLAIN, 22));
		txtRan.setColumns(10);
		txtRan.setBounds(364, 240, 90, 30);
		contentPane.add(txtRan);

		JCheckBox txtCheck = new JCheckBox("记住密码");
		txtCheck.setFont(new Font("宋体", Font.PLAIN, 16));
		txtCheck.setBounds(355, 324, 101, 27);
		contentPane.add(txtCheck);

		JLabel label = new JLabel("密  码");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(149, 174, 80, 33);
		contentPane.add(label);

		JButton btnNewButton = new JButton("登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				// 获取用户输入的值
				String username = textUsername.getText();
				// 返回字符数组
				char[] tpassword = textPassword.getPassword();
				// 转化为字符串
				String password = new String(tpassword);
				// 验证码
				String rancode = txtRanCode.getText();
				String ran = txtRan.getText();

				// 对输入值进行判断
				if (username.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "用户名不能为空");
				} else if (password.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "密码不能为空");
				} else if (rancode.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "验证码不能为空");
				} else {
					if (rancode.equals(ran)) {
						boolean flag = false;
						//表示某一用户
						int radio = 0;
						if (radioButton_user.isSelected()) {
							// 创建对象
							UserDao ud = new UserDao();
							flag = ud.dologin(username, password);
							// 判断是否登录成功
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "登录成功");
								// 传值
								int select = 0;
								// 如果记住密码被选中select为1
								if (txtCheck.isSelected()) {
									select = 1;
								}
								//普通员工被选中
								radio=1;
								User.setInfo(username, password,select,radio);
								// 跳转到菜单框
								MenuFrameUser mfu = new MenuFrameUser(username);
								// 显示该窗口
								mfu.setVisible(true);
								// 隐藏当前窗口
								setVisible(false);
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "用户名或密码错误，登录失败");
								// 更新验证码
								txtRan.setText(RandomCodeTools.getRan());
								// 清空密码框
								textPassword.setText("");
								txtRanCode.setText("");
							}
						} else if (radioButton_dadmin.isSelected()) {// 仓库管理员被选中
							// 创建对象
							Device_AdminDao dad = new Device_AdminDao();
							flag = dad.dologin(username, password);
							// 判断是否登录成功
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "登录成功");
								// 传值
								int select = 0;
								// 如果记住密码被选中n为1
								if (txtCheck.isSelected()) {
									select = 1;
								}
								radio=2;
								User.setInfo(username, password, select,radio);
								// 跳转到菜单框
								MenuFrameDevAdmin mf = new MenuFrameDevAdmin(username);
								// 显示该窗口
								mf.setVisible(true);
								// 隐藏当前窗口
								setVisible(false);
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "用户名或密码错误，登录失败");
								// 更新验证码
								txtRan.setText(RandomCodeTools.getRan());
								// 清空密码框
								textPassword.setText("");
								txtRanCode.setText("");
							}

						} else if (radioButton_admin.isSelected()) {
							// 创建对象
							AdminDao ad = new AdminDao();
							flag = ad.dologin(username, password);
							// 判断是否登录成功
							if (flag) {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "登录成功");
								// 传值
								int select = 0;
								// 如果记住密码被选中select为1
								if (txtCheck.isSelected()) {
									select = 1;
								}
								radio=3;
								User.setInfo(username, password, select,radio);
								// 跳转到菜单框
								MenuFrameAdmin mfa = new MenuFrameAdmin(username);
								// 显示该窗口
								mfa.setVisible(true);
								// 隐藏当前窗口
								setVisible(false);
							} else {
								// 弹窗提示
								JOptionPane.showMessageDialog(null, "用户名或密码错误，登录失败");
								// 更新验证码
								txtRan.setText(RandomCodeTools.getRan());
								// 清空密码框
								textPassword.setText("");
								txtRanCode.setText("");
							}
						}
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "验证码错误");
					}

				}

			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));
		btnNewButton.setBounds(351, 363, 110, 30);
		contentPane.add(btnNewButton);

		JButton button = new JButton("重置");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textUsername.setText("");
				textPassword.setText("");
				txtRanCode.setText("");
				txtRan.setText(RandomCodeTools.getRan());
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.setBounds(167, 363, 110, 30);
		contentPane.add(button);
		
		//对记住密码进行判断
		// 普通用户取值
		User user =User.getInfo();
		if (user==null) {
			textUsername.setText("");
			textPassword.setText("");
		}else if (user.getRadio()==1) {
			if (user.getSelect()==0) {
				textUsername.setText(user.getUsername());
			}else if (user.getSelect()==1) {
				textUsername.setText(user.getUsername());
				textPassword.setText(user.getPassword());
				//设置复选框为打钩
				txtCheck.setSelected(true);
				//普通员工已默认打钩
			}
		}else if (user.getRadio()==2) {
			if (user.getSelect()==0) {
				textUsername.setText(user.getUsername());
				//给仓库员工打钩
				radioButton_dadmin.setSelected(true);
			}else if (user.getSelect()==1) {
				textUsername.setText(user.getUsername());
				textPassword.setText(user.getPassword());
				//设置复选框为打钩
				txtCheck.setSelected(true);
				//给仓库员工打钩
				radioButton_dadmin.setSelected(true);
			}
			
		}else if (user.getRadio()==3) {
			if (user.getSelect()==0) {
				textUsername.setText(user.getUsername());
				//给管理员打钩
				radioButton_admin.setSelected(true);
			}else if (user.getSelect()==1) {
				textUsername.setText(user.getUsername());
				textPassword.setText(user.getPassword());
				//设置复选框为打钩
				txtCheck.setSelected(true);
				//给管理员打钩
				radioButton_admin.setSelected(true);
			}
			
		}

		// 居中
		setLocationRelativeTo(null);
	}
}
